clc;
clear;

x = [8,3, 4,5,23,4,12,13,14,15,33,4,7,94,45,46,47];  
[start, count, avg] = find(x);  
disp('连续值的开始下标:');    disp(start);  
disp('每个连续值的数量:');    disp(count);  
disp('每个连续值的平均值:'); disp(avg);

function [start, count, avg] = find(x)  
    start = []; % 存储连续值的开始下标  
    count = []; % 存储每个连续值的数量  
    avg = []; % 存储每个连续值的平均值  
    prev_val = x(1); % 初始化前一个值为向量的第一个元素  
    curr_start = 1; % 当前连续值的开始下标  
    count_consecutive = 1; % 当前连续值的数量  
  
    for i = 2:length(x)  
        if x(i) == x(i-1) + 1
            count_consecutive = count_consecutive + 1;  
        else  
            start = [start curr_start]; % 添加新的开始下标到列表中  
            count = [count count_consecutive]; % 添加新的数量到列表中  
            avg = [avg (sum(x(curr_start:i-1))/count_consecutive)]; % 添加新的平均值到列表中  
            prev_val = x(i); % 更新前一个值为当前元素  
            curr_start = i; % 更新当前连续值的开始下标为当前下标  
            count_consecutive = 1; % 重置连续值的数量  
        end  
    end  
    start = [start curr_start]; % 处理最后一个连续序列  
    count = [count count_consecutive];   
    avg = [avg (sum(x(curr_start:end))/count_consecutive)]; % 添加最后一个连续序列的平均值  
end
